#!/usr/bin/env bash
# Copyright (c) 2022 Presto Labs Pte. Ltd.
# Author: ziyan

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(git rev-parse --show-toplevel)"
LOG_DIR="${REPO_ROOT}/../log"

DATETIME="$(date +%Y%m%d-%H%M%S%z)"
LOG_FILE="log_download_s3_iosg_line_priority.${DATETIME}"

mkdir -p "${LOG_DIR}"
cd "${REPO_ROOT}"
cd python

# source "${SCRIPT_DIR}/py_bootstrap.sh"
source ~/miniconda3/etc/profile.d/conda.sh 
conda activate coin2_motion_env

source ../../coin_deploy/support_feed_coin2/aws_key/feed.raw.coin2_reader.sh

MACHINES=(
    # "feed-01.ap-northeast-1.aws.huobi"
    # "feed-02.us-east-1.aws"
    # "feed-02.eu-west-1.aws"
    # "feed-02.ap-northeast-2.aws"
    "feed-05.ap-northeast-1.aws"
    "feed-05.cn-hongkong.aliyun"
    # "feed-10.ap-northeast-1.aws"
)

TARGET_DATE="$(date --utc --date='30 minutes ago' '+%Y%m%d')"


BUCKET="feed.raw.coin2.corp.prestolabs.io"

# https://docs.aws.amazon.com/cli/latest/topic/s3-config.html
aws configure set default.s3.max_bandwidth 200MB/s

# https://github.com/aws/aws-cli/pull/2747
OPTIONS="--no-progress"

echo "BUCKET='${BUCKET}'"
date

while : ; do
  for machine in ${MACHINES[*]}
  do
    case $machine in
      "feed-05.ap-northeast-1.aws" )
        EXCHANGES=("spot_binance_v1" "futures_binance_v1" "spot_okex_v5" "futures_okex_v5-swap" "spot_bybit_v3" "futures_bybit_v3-linear")
        ;;
      "feed-05.cn-hongkong.aliyun")
        EXCHANGES=("spot_coingecko_v3")
    esac
    for mea in ${EXCHANGES[*]}
    do
      ./pyrunner coin/support/feed_archive/app/remove_s3_sync_tmp_files.py \
          --machine=${machine} \
          --date=${TARGET_DATE}
      SRC_DIR="s3://${BUCKET}/data/flow/${machine}/${TARGET_DATE}/${mea}"
      DST_DIR="/export/iosg/coin-mirror-1/buckets/feed.raw.coin/${machine}/${TARGET_DATE}/${mea}"
      echo ${SRC_DIR} ${DST_DIR}
      aws s3 sync --delete ${OPTIONS} "${SRC_DIR}" "${DST_DIR}" \
          2>> "${LOG_DIR}/${LOG_FILE}"
    done
  done
  ./pyrunner coin/support/feed_archive/app/check_sync_completion.py
  if [[ $? == 0 ]]
  then 
      echo "sync done"
      break;
  else 
      echo "sync not compelete, will try 1min later"
      sleep 60s 
  fi
done

date
